package controlador;

import java.awt.event.KeyEvent;
import java.util.Date;
import objetos.aeronaves.Algo42;
import ar.uba.fi.algo3.titiritero.KeyPressedObservador;

/*
 * Clase que controla la entrada por teclado de los distintos comandos del Algo42
 */
public class ControladorAlgo42 implements KeyPressedObservador {
	private Algo42 algo42;
	/* Usados para el delay entre disparo y disparo */
	private Date lastShoot = new Date();
	private long LASER_DELAY = 200;
	private long COHETE_DELAY = 500;
	private boolean[] keysPressed = new boolean[256];

	public ControladorAlgo42(Algo42 algo42) {
		this.algo42 = algo42;
	}

	public void keyPressed(KeyEvent e) {
		keysPressed[e.getKeyCode()] = true;
		this.actOnKeys();
	}

	public void keyReleased(KeyEvent e) {
		keysPressed[e.getKeyCode()] = false;
		this.actOnKeys();
	}

	public void actOnKeys() {
		Date actual = new Date();
		if (keysPressed[KeyEvent.VK_RIGHT]) {
			this.algo42.moverDerecha();
		}
		if (keysPressed[KeyEvent.VK_LEFT]) {
			this.algo42.moverIzquierda();
		}
		if (keysPressed[KeyEvent.VK_UP]) {
			this.algo42.moverArriba();
		}
		if (keysPressed[KeyEvent.VK_DOWN]) {
			this.algo42.moverAbajo();
		}
		if (keysPressed[KeyEvent.VK_SPACE]) {
			if (this.lastShoot.getTime() + LASER_DELAY < actual.getTime()) {
				this.algo42.dispararLaser();
				this.lastShoot = actual;
			}
		}
		if (keysPressed[KeyEvent.VK_Q]) {
			if (this.lastShoot.getTime() + COHETE_DELAY < actual.getTime()) {
				this.algo42.dispararCohetera();
				this.lastShoot = actual;
			}
		}
		if (keysPressed[KeyEvent.VK_W]) {
			if (this.lastShoot.getTime() + COHETE_DELAY < actual.getTime()) {
				this.algo42.dispararTorpedo();
				this.lastShoot = actual;
			}
		}
		if (keysPressed[KeyEvent.VK_E]) {
			if (this.lastShoot.getTime() + COHETE_DELAY < actual.getTime()) {
				this.algo42.dispararRastreador();
				this.lastShoot = actual;
			}
		}
		if (keysPressed[KeyEvent.VK_R]) {
			if (this.lastShoot.getTime() + COHETE_DELAY < actual.getTime()) {
				this.algo42.dispararAdaptable();
				this.lastShoot = actual;
			}
		}
		if (keysPressed[KeyEvent.VK_D]) {
			this.algo42.darVuelta();
		}
	}

}
